package common

import (
	"gorm.io/gorm"
	"hobby-mate/cache"
	"time"
)

// BaseEntity 基础实体
type BaseEntity struct {
	CreateBy     string         `json:"createBy" form:"create_by"  gorm:"not null;comment:创建人"`
	UpdateBy     string         `json:"updateBy" form:"update_by" gorm:"comment:更新人"`
	DeleteBy     string         `json:"deleteBy" form:"delete_by" gorm:"comment:删除人"`
	CreateByName string         `json:"createByName" form:"create_by_name"  gorm:"not null;comment:创建名称"`
	UpdateByName string         `json:"updateByName" form:"update_by_name"  gorm:"comment:更新人名字"`
	DeleteByName string         `json:"deleteByName" form:"delete_by_name"  gorm:"comment:删除人名字"`
	CreatedAt    time.Time      `json:"createdTime" form:"-" gorm:"not null;comment:创建时间"`
	UpdatedAt    time.Time      `json:"updatedTime" form:"-" gorm:"comment:更新时间"`
	DeleteAt     gorm.DeletedAt `json:"deleteTime"  form:"-" gorm:"comment:软删除时间"`
}

// InsertSetBaseEntity 设置BaseEntity的创建人
func (b *BaseEntity) InsertSetBaseEntity() {
	b.CreateBy = cache.UserInfos.AccountId
	b.CreateByName = cache.UserInfos.Username
	b.CreatedAt = time.Now()
}

// UpdateSetBaseEntity 设置BaseEntity的更新人
func (b *BaseEntity) UpdateSetBaseEntity() {
	//b.UpdateBy = cache.UserInfos.AccountId
	//b.UpdateByName = cache.UserInfos.Username
	b.UpdatedAt = time.Now()
}

// DeleteSetBaseEntity 设置BaseEntity的删除人
func (b *BaseEntity) DeleteSetBaseEntity() {
	//b.DeleteBy = cache.UserInfos.AccountId
	//b.DeleteByName = cache.UserInfos.Username
	b.DeleteAt = gorm.DeletedAt{Time: time.Now()}
}
